package display;

import option.Data;
import option.Sweeper;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

class SetListener implements ActionListener {
    public static int x, y, leiCount;

    /**
     * 设置数据，开始新游戏
     **/
    @Override
    public void actionPerformed(ActionEvent e) {
        try {
            if (OptionFrame.jTextField1.isEnabled()) {
                int x = Integer.parseInt(OptionFrame.jTextField1.getText());
                int y = Integer.parseInt(OptionFrame.jTextField2.getText());
                int leiCount = Integer.parseInt(OptionFrame.jTextField3.getText());
                if (x < 9 || x > 36 || y < 9 || y > 32 || leiCount < 10 || x * y <= leiCount) {
                    JOptionPane.showMessageDialog(MenuPanel.optionFrame, "非法值，请重新输入！", "输入错误", JOptionPane.ERROR_MESSAGE);
                    return;
                }
                SetListener.x = x;
                SetListener.y = y;
                SetListener.leiCount = leiCount;
            }
            Data.x = x;
            Data.y = y;
            Data.leiCount = leiCount;
            Sweeper.newGame();
            MenuPanel.optionFrame.setVisible(false);
        } catch (Exception ex) {
            JOptionPane.showMessageDialog(MenuPanel.optionFrame, "非法值，请重新输入！", "输入错误", JOptionPane.ERROR_MESSAGE);
        }
    }
}
